/* *****************************************************************************
 *  Name:              Ada Lovelace
 *  Coursera User ID:  123456
 *  Last modified:     October 16, 1842
 **************************************************************************** */

/*
Using algs4.jar. Under construction. Write a program RandomWord.java that reads
a sequence of words from standard input and prints one of those words uniformly
at random. Do not store the words in an array or list. Instead, use Knuth’s method:
when reading the ith word, select it with probability 1/i to be the champion,
replacing the previous champion. After reading all of the words, print the surviving champion.
 */

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class RandomWord {
    public static void main(String[] args) {
        String readIn;
        int index = 0;
        String champion = "";
        while (!StdIn.isEmpty()) {
            readIn = StdIn.readString();
            index++;
            if (StdRandom.bernoulli(1.0 / index)) {
                champion = readIn;
            }
        }
        StdOut.println(champion);
    }

    // public static void main(String[] args) {
    //     for (int i = 0; i < 10; i++) {
    //         System.out.print(StdRandom.bernoulli(0.1));
    //     }
    // }
}
